﻿using System;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FleetClientTest
{
	public class Client
	{
		public static Object _lockObject = new Object();

		public static void Connect(string serverIP, string message)
		{
			try
			{
				// Random daytime port :P
				Int32 port = 13;
				TcpClient client = new TcpClient(serverIP, port);

				Byte[] data = new Byte[256];
				data = System.Text.Encoding.ASCII.GetBytes(message);

				lock (_lockObject)
				{
					// Send the data through the network.
					NetworkStream stream = client.GetStream();
					stream.Write(data, 0, data.Length);

					Console.WriteLine(string.Format("Sent: {0}", message));

					data = new Byte[256];

					string response = string.Empty;
					Int32 bytes = stream.Read(data, 0, data.Length);
					response = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
					Console.WriteLine(string.Format("Server Responded: {0},", response));

					stream.Close();
				}

				client.Close();
			}
			  catch (ArgumentNullException e)
            {
				Console.WriteLine("ArgumentNullException: " + e);
            }
            catch (SocketException e)
            {
				Console.WriteLine("SocketException: " + e);
            }
			catch(Exception e)
			{
				Console.WriteLine("Exception: " + e);
			}
		}
	}
}
